//
//  ConsciousBreathing.swift
//  HIextractLicve
//
//  Created by HIextractLicve on 2024/11/12.
//

import UIKit
import SnapKit
import MJRefresh
import ProgressHUD
let saaffeeTppoHightHTL = UIApplication.shared.statusBarFrame.height

var bommbsafeHeitHTL:CGFloat{
    if let window = UIApplication.shared.windows.first
           {
        let safeAreaInsets = window.safeAreaInsets
           return safeAreaInsets.bottom
       }
       return 0.0
}

extension Double {
   
    var xHTL: CGFloat {
        return UIScreen.main.bounds.width * CGFloat(self) / 375.0
    }
    var yHTL: CGFloat {
        return UIScreen.main.bounds.height * CGFloat(self) / 812.0
    }
}


class ConsciousBreathing: HTLCBasecompression {
    var mindfulness:Set<Int>?
    var suggestion:Set<String>?
    var  professionals:Set<CGFloat>?
    var exchangeHTLC:[String:Array<Dictionary<String,String>>]?

    
    var eachVioViewHTLC:THLCNOrBUTTOn?
    var mindfulnessPageHTL:THLCNPageControll?
    var journeyHTLC:UIView?
    
    static var agreenELUHTL:Bool = false
    static var showedELUHTL:Bool = false
    
    
    let nextButtonHTL = UIButton.init()
    let screoolbire = UIScrollView.init()
    var hreingpickTager:Int = 0
    
    var maingkingNOwRoomDAtaHTLC:Array<Dictionary<String,String>>{
        
      
     
        
        if hreingpickTager == 0 {
           
            return HTLCAllDealRelaxing.Relaxing.foryourDataHTL.0
        }else  if hreingpickTager == 1 {
           
            return HTLCAllDealRelaxing.Relaxing.trendDataHTL.0
        }else  if hreingpickTager == 2 {
           
            return HTLCAllDealRelaxing.Relaxing.mediDataHTL.0
        }else  if hreingpickTager == 3 {
            
            return HTLCAllDealRelaxing.Relaxing.artDataHTL.0
        }else  {
            
            return HTLCAllDealRelaxing.Relaxing.natureDataHTL.0
        }
    }
    
    
    lazy var foryoubuttonHTL = createTopTypeButtonHTL(setitle:"For you",selImage:"foryou_sel",normalImag:"foryou_un",tag:0)
    lazy var TrendingbuttonHTL = createTopTypeButtonHTL(setitle:"Trending",selImage:"trend_sel",normalImag:"trend_un",tag:1)
    lazy var MeditationbuttonHTL = createTopTypeButtonHTL(setitle:"Meditation",selImage:"med_sel",normalImag:"med_un",tag:2)
    lazy var ArtbuttonHTL = createTopTypeButtonHTL(setitle:"Art",selImage:"art_sel",normalImag:"art_un",tag:0)
    lazy var NaturebuttonHTL = createTopTypeButtonHTL(setitle:"Nature",selImage:"nature_sel",normalImag:"nature_un",tag:0)
    
    lazy var createbujiHTL = UIButton.init()
    
    
    
    
    func createTopTypeButtonHTL(setitle:String,selImage:String,normalImag:String,tag:Int)->UIButton  {
        let foKKKji = UIButton.init()
        foKKKji.setTitle(" " + setitle, for: .normal)
        foKKKji.setTitleColor(.white, for: .normal)
        foKKKji.titleLabel?.font = UIFont(name: "Trebuchet MS", size: 15)
        foKKKji.setImage(UIImage.init(named: normalImag), for: .normal)
        foKKKji.setImage(UIImage.init(named: selImage), for: .selected)
        foKKKji.tag = tag
        foKKKji.layer.cornerRadius = 19
        foKKKji.layer.masksToBounds = true
        foKKKji.backgroundColor = UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
        return foKKKji
    }
    
    
    
    lazy var builCollectionLiveViewHTL: UICollectionView = {
        let coijfLayout = UICollectionViewFlowLayout.init()
        coijfLayout.itemSize = CGSize.init(width: (UIScreen.main.bounds.width - 36)/2, height:275)
        
        coijfLayout.minimumInteritemSpacing = 12
        coijfLayout.scrollDirection = .vertical
        coijfLayout.minimumLineSpacing = 25
        
        
        let fiettCol = UICollectionView.init(frame: .zero, collectionViewLayout: coijfLayout)
        let nib = UINib(nibName: "HTLCDisgistLiceListCell", bundle: nil)
               
        fiettCol.register(nib, forCellWithReuseIdentifier: "HTLCDisgistLiceListCellID")
             
        fiettCol.delegate = self
        fiettCol.dataSource = self
     
            fiettCol.showsVerticalScrollIndicator = false
        
       
        fiettCol.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 310, right: 0)
       fiettCol.showsVerticalScrollIndicator = false
       
        fiettCol.backgroundColor = .clear
        fiettCol.dataSource = self
        fiettCol.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                fiettCol.reloadData()
               
                    fiettCol.mj_header?.endRefreshing()
                
                
            }
            
        })
        return fiettCol
    }()
    
    
    @objc func bloaaalackSomentoneRemoveHTLC()  {
        builCollectionLiveViewHTL.reloadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        mindfulness = [99]
        suggestion = ["decompression"]
        professionals = [12.1]
        exchangeHTLC = ["themtopicHTLC":[["decompression":"098"]]]

        NotificationCenter.default.addObserver(self, selector: #selector(bloaaalackSomentoneRemoveHTLC), name: NSNotification.Name("htldeleteuser"), object: nil)
        
        let kopImgf = UIImageView.init(image: UIImage.init(named: "Hilfy_hometitle"))
        kopImgf.contentMode = .scaleToFill
        view.addSubview(kopImgf)
        kopImgf.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(saaffeeTppoHightHTL + 14.yHTL)
            make.leading.equalToSuperview().offset(16.xHTL)
            make.width.equalTo(92.xHTL)
            make.height.equalTo(55.xHTL)
        }
        
        let nortyiButton = UIButton.init()
        nortyiButton.setImage(UIImage.init(named: "notify_hTL"), for: .normal)
        nortyiButton.addTarget(self, action: #selector(gotoNotiMeaassgeHTLC), for: .touchUpInside)
        view.addSubview(nortyiButton)
        nortyiButton.snp.makeConstraints { make in
            make.centerY.equalTo(kopImgf)
            make.trailing.equalToSuperview().inset(12.xHTL)
            make.width.equalTo(38.xHTL)
            make.height.equalTo(38.xHTL)
        }
        
        
        let aichatButton = UIImageView.init(image: UIImage.init(named: "roqiren_hometitle"))
        view.addSubview(aichatButton)
        aichatButton.snp.makeConstraints { make in
           
            make.leading.equalToSuperview().inset(4.xHTL)
            make.width.equalTo(303.xHTL)
            make.height.equalTo(106.xHTL)
            make.top.equalTo(kopImgf.snp.bottom).offset(11)
        }
        
        
        nextButtonHTL.setImage(UIImage.init(named: "go_hometitle"), for: .normal)
        nextButtonHTL.addTarget(self, action: #selector(gogonextTonmesglistHTL(hijkBUtton:)), for: .touchUpInside)
        view.addSubview(nextButtonHTL)
        nextButtonHTL.snp.makeConstraints { make in
           
            make.trailing.equalToSuperview().inset(12.xHTL)
            make.leading.equalTo(aichatButton.snp.trailing).offset(8.xHTL)
            make.height.equalTo(93.xHTL)
            make.bottom.equalTo(aichatButton.snp.bottom)
        }
        
        
        createcenterScreooloviewHTL()
        createbujiHTL.setImage(UIImage.init(named: "homeTlepub"), for: .normal)
        createbujiHTL.addTarget(self, action: #selector(setuppostloviewHTL), for: .touchUpInside)
       
        self.view.addSubview(self.builCollectionLiveViewHTL)
        self.builCollectionLiveViewHTL.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(12)
            make.bottom.equalToSuperview()
            make.top.equalTo(self.screoolbire.snp.bottom).offset(20.xHTL)
        }
        
        self.builCollectionLiveViewHTL.mj_header?.beginRefreshing()
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.view.addSubview(self.createbujiHTL)
            self.mindfulness = [99]
            self.suggestion = ["decompression"]
            self.professionals = [12.1]
            self.exchangeHTLC = ["themtopicHTLC":[["decompression":"098"]]]

            self.createbujiHTL.snp.makeConstraints { make in
                make.width.height.equalTo(58)
                make.right.equalToSuperview().inset(10)
                make.bottom.equalToSuperview().offset(-(self.tabBarController?.tabBar.frame.height ?? 90) - 12)
            }
        }
        
       
        
    }
    
    @objc func setuppostloviewHTL() {
        if self.navigationController?.navigationBar.isHidden == true {
            journeyHTLC?.alpha = 1
            journeyHTLC?.layer.cornerRadius = 12
        }
        let ponew = PositivePsychology.init()
        self.navigationController?.pushViewController(ponew, animated: true)
    }
    
    func createcenterScreooloviewHTL() {
        screoolbire.showsHorizontalScrollIndicator = false
        screoolbire.backgroundColor = .clear
        view.addSubview(screoolbire)
        screoolbire.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(12.xHTL)
            make.height.equalTo(38)
            make.right.equalToSuperview()
            make.top.equalTo(nextButtonHTL.snp.bottom).offset(31.xHTL)
        }
        foryoubuttonHTL.isSelected = true
        foryoubuttonHTL.backgroundColor = UIColor(red: 1, green: 0.01, blue: 0.33, alpha: 1)
        
        foryoubuttonHTL.addTarget(self, action: #selector(refrehsgtHTL(hijkBUtton:)), for: .touchUpInside)
        TrendingbuttonHTL.addTarget(self, action: #selector(refrehsgtHTL(hijkBUtton:)), for: .touchUpInside)
        MeditationbuttonHTL.addTarget(self, action: #selector(refrehsgtHTL(hijkBUtton:)), for: .touchUpInside)
        ArtbuttonHTL.addTarget(self, action: #selector(refrehsgtHTL(hijkBUtton:)), for: .touchUpInside)
        NaturebuttonHTL.addTarget(self, action: #selector(refrehsgtHTL(hijkBUtton:)), for: .touchUpInside)
        
        screoolbire.addSubview(foryoubuttonHTL)
        screoolbire.addSubview(TrendingbuttonHTL)
        screoolbire.addSubview(MeditationbuttonHTL)
        screoolbire.addSubview(ArtbuttonHTL)
        screoolbire.addSubview(NaturebuttonHTL)
        
        foryoubuttonHTL.snp.makeConstraints { make in
            make.width.equalTo(96)
            make.height.equalTo(38)
            make.leading.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        TrendingbuttonHTL.snp.makeConstraints { make in
            make.width.equalTo(109)
            make.height.equalTo(38)
            make.leading.equalTo(foryoubuttonHTL.snp.trailing).offset(12.xHTL)
            make.top.bottom.equalToSuperview()
        }
        
        MeditationbuttonHTL.snp.makeConstraints { make in
            make.width.equalTo(129)
            make.height.equalTo(38)
            make.leading.equalTo(TrendingbuttonHTL.snp.trailing).offset(12.xHTL)
            make.top.bottom.equalToSuperview()
        }
        
        ArtbuttonHTL.snp.makeConstraints { make in
            make.width.equalTo(70)
            make.height.equalTo(38)
            make.leading.equalTo(MeditationbuttonHTL.snp.trailing).offset(12.xHTL)
            make.top.bottom.equalToSuperview()
        }
        
        NaturebuttonHTL.snp.makeConstraints { make in
            make.width.equalTo(94)
            make.height.equalTo(38)
            make.leading.equalTo(ArtbuttonHTL.snp.trailing).offset(12.xHTL)
            make.trailing.equalToSuperview().offset(-200)
            make.top.bottom.equalToSuperview()
        }
        
       
        
    }
    
   @objc func gotoNotiMeaassgeHTLC()  {
       if self.navigationController?.navigationBar.isHidden == true {
           journeyHTLC?.alpha = 1
           journeyHTLC?.layer.cornerRadius = 12
       }
       self.navigationController?.pushViewController(EbanotionalResonance.init(), animated: true)
    }
    
    @objc func gogonextTonmesglistHTL(hijkBUtton:UIButton)  {
        if HTLCAllDealRelaxing.Relaxing.setChatAiHTL.aiChatNameHTL == nil {
           
            let Hcagtg = TeherapeuticTouch.init()
            
            self.navigationController?.pushViewController(Hcagtg, animated: true)
            return
        }else{
            let Hcagtg = StorytellingHealing.init()
            
            self.navigationController?.pushViewController(Hcagtg, animated: true)
            
            
        }
    }
    

}


extension ConsciousBreathing:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return maingkingNOwRoomDAtaHTLC.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let passonHtlacData = maingkingNOwRoomDAtaHTLC[indexPath.row]
        
        let fffuarcell = collectionView.dequeueReusableCell(withReuseIdentifier: "HTLCDisgistLiceListCellID", for: indexPath) as! HTLCDisgistLiceListCell
      
      
            fffuarcell.htlSeeCountlfbl.text = passonHtlacData["HtlcViewCoiutn"]
            fffuarcell.livetitkleHTL.text =  passonHtlacData["HTLLiveTitle"]
        
      
        fffuarcell.liveCVoverHTL.image = UIImage(named: passonHtlacData["HTLLiveCover"]  ?? "")
        fffuarcell.userHeaderHTL.image = UIImage(named: passonHtlacData["HTLCAvater"]  ?? "")
        fffuarcell.usernamhrHTl.text = passonHtlacData["HTLCkiName"]
        let customized = "1customized0".components(separatedBy: "customized")
        
        fffuarcell.userfolloewrCoutHTL.text = (passonHtlacData["HTLCFollowercount"] ?? customized[1]) + " Followers"
        return fffuarcell
        
        
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let passonHtlacData = maingkingNOwRoomDAtaHTLC[indexPath.row]
        var huihuo = 43
        huihuo += 1
        var pagenorHTLC = 23
        pagenorHTLC -= 2
        let community:Set<Int> = [huihuo,pagenorHTLC]
        let enterlive = SlowMovement.init(stressdescHTL: community, liveHTLCusserdata: passonHtlacData)
        self.navigationController?.pushViewController(enterlive, animated: true)
    }
    
    
    @objc func refrehsgtHTL(hijkBUtton:UIButton)  {
        
        
        if hijkBUtton == self.foryoubuttonHTL {
            hreingpickTager = 0
            foryoubuttonHTL.isSelected = true
            foryoubuttonHTL.backgroundColor =  UIColor(red: 1, green: 0.01, blue: 0.33, alpha: 1)
            
            TrendingbuttonHTL.isSelected = false
            TrendingbuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            MeditationbuttonHTL.isSelected = false
            MeditationbuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            ArtbuttonHTL.isSelected = false
            ArtbuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            
            NaturebuttonHTL.isSelected = false
            NaturebuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
        }
    
        
        
        if hijkBUtton == self.TrendingbuttonHTL {
            hreingpickTager = 1
            TrendingbuttonHTL.isSelected = true
            TrendingbuttonHTL.backgroundColor =  UIColor(red: 1, green: 0.01, blue: 0.33, alpha: 1)
            
            foryoubuttonHTL.isSelected = false
            foryoubuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            MeditationbuttonHTL.isSelected = false
            MeditationbuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            ArtbuttonHTL.isSelected = false
            ArtbuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            
            NaturebuttonHTL.isSelected = false
            NaturebuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
        }
        
        
        if hijkBUtton == self.MeditationbuttonHTL {
            hreingpickTager = 2
            MeditationbuttonHTL.isSelected = true
            MeditationbuttonHTL.backgroundColor =  UIColor(red: 1, green: 0.01, blue: 0.33, alpha: 1)
            
            TrendingbuttonHTL.isSelected = false
            TrendingbuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            foryoubuttonHTL.isSelected = false
            foryoubuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            ArtbuttonHTL.isSelected = false
            ArtbuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            
            NaturebuttonHTL.isSelected = false
            NaturebuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
        }
        
        if hijkBUtton == self.ArtbuttonHTL {
            hreingpickTager = 3
            ArtbuttonHTL.isSelected = true
            ArtbuttonHTL.backgroundColor =  UIColor(red: 1, green: 0.01, blue: 0.33, alpha: 1)
            
            TrendingbuttonHTL.isSelected = false
            TrendingbuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            MeditationbuttonHTL.isSelected = false
            MeditationbuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            foryoubuttonHTL.isSelected = false
            foryoubuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            
            NaturebuttonHTL.isSelected = false
            NaturebuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
        }
        
        if hijkBUtton == self.NaturebuttonHTL {
            hreingpickTager = 4
            NaturebuttonHTL.isSelected = true
            NaturebuttonHTL.backgroundColor =  UIColor(red: 1, green: 0.01, blue: 0.33, alpha: 1)
            
            TrendingbuttonHTL.isSelected = false
            TrendingbuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            MeditationbuttonHTL.isSelected = false
            MeditationbuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            ArtbuttonHTL.isSelected = false
            ArtbuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
            
            
            foryoubuttonHTL.isSelected = false
            foryoubuttonHTL.backgroundColor =  UIColor(red: 0.16, green: 0.16, blue: 0.16, alpha: 1)
        }
        
        self.builCollectionLiveViewHTL.mj_header?.beginRefreshing()
    }
    
}
